//
//  TestCenterVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/14.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "TestCenterVC.h"
#import "testCenterCell.h"
#import "pickCelll.h"
#import "TestCenterInforVC.h"

@interface TestCenterVC ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UILabel *title_lab;
@property (strong, nonatomic) IBOutlet UIButton *choose_btn;
@property (strong, nonatomic) IBOutlet UITableView *table;
@property (nonatomic , strong) UITableView *pickTable;
@end

@implementation TestCenterVC{
    NSString *chooseStr;//选择的类型
    
    RequestServices *service;
    NSInteger page_index;//页码
    NSMutableArray *list_arr;//列表数组
    NSString *chooseType;//试验类型 0 默认全部 1：互动试验 2：模型展示
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"试验中心";
    APPViewBorderRadius(self.choose_btn, 5, 1, [UIColor blackColor]);
    self.choose_btn.titleLabel.font = self.title_lab.font = AutoLabFont2;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table registerNib:[UINib nibWithNibName:@"testCenterCell" bundle:nil] forCellReuseIdentifier:@"testCenterCell"];
        
    //接收通知1
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeBtnTitle:) name:@"passChooseStr" object:nil];
    //选择类型
    [self.choose_btn setTitle:@"全部" forState:UIControlStateNormal];
    [self.choose_btn addTarget:self action:@selector(showPick) forControlEvents:UIControlEventTouchUpInside];
    _contentArr = [NSArray arrayWithObjects:@"全部",@"互动试验",@"模型展示",nil];
    chooseType = @"0";
    
    
    list_arr = [[NSMutableArray alloc]init];
    service = [[RequestServices alloc]initWithDelegate:self];
    service.view = self.view;
    __weak __typeof(self)weakSelf = self;
    [self headerRefresh];
    self.table.mj_header = [baseRefreshHeader headerWithRefreshingBlock:^{
        [weakSelf headerRefresh];
    }];
    self.table.mj_footer = [baseRefreshFooter footerWithRefreshingBlock:^{
        [weakSelf footerRefresh];
    }];
    self.table.mj_footer.hidden = YES;
}

- (void)headerRefresh
{
    page_index = 1;
    [service getTestCenterList:[NSString stringWithFormat:@"%ld",page_index] type:chooseType tag:8000];
}

- (void)footerRefresh
{
    page_index++;
    [service getTestCenterList:[NSString stringWithFormat:@"%ld",page_index] type:chooseType tag:8001];
}


#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self.table.mj_footer endRefreshing];
    [self.table.mj_header endRefreshing];
    
    [self HideerrorView];
    [self HideNoDataView];
    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:obj[@"list"]];
    if (tag == 8000) {
        if ([tempArr CheckNSArrayData]) {
            list_arr = [TestCenterModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];

        }
        if (tempArr.count == 0) {
            [self ShowNoDataView:self.table Orgin:0];
        }
    }else {
        [list_arr addObjectsFromArray:[TestCenterModel mj_objectArrayWithKeyValuesArray:tempArr]];
    }
    
    [self.table reloadData];
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self.table.mj_footer endRefreshing];
    [self.table.mj_header endRefreshing];
    [self ShowNewMsg:error.domain];
    if (tag == 8000) {
        [self ShowerrorView:self.table Orgin:0];
    }
}




//获取选择后的值
-(void)changeBtnTitle:(NSNotification *)notification{
    [self.choose_btn setTitle:[NSString stringWithFormat:@"%@",notification.userInfo[@"getStr"]] forState:UIControlStateNormal];
    NSString *typeStr = [NSString stringWithFormat:@"%@",notification.userInfo[@"getStr"]];
    if ([typeStr isEqualToString:@"互动试验"]) {
        chooseType = @"1";
    }else if ([typeStr isEqualToString:@"模型展示"]){
        chooseType = @"2";
    }else{
        chooseType = @"0";
    }
    [service getTestCenterList:[NSString stringWithFormat:@"%ld",page_index] type:chooseType tag:8000];
}

//选择类型
-(void)showPick{
    self.pickTable = [[UITableView alloc]init];
    self.pickTable.tag = 10000;
    self.pickTable.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.pickTable];
    [self.pickTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.choose_btn.mas_top);
        make.left.equalTo(self.choose_btn);
        make.width.equalTo(self.choose_btn);
        make.height.mas_equalTo(self.choose_btn.frame.size.height * _contentArr.count);
   }];
    
    self.pickTable.delegate = self;
    self.pickTable.dataSource = self;
    [self.pickTable registerNib:[UINib nibWithNibName:@"pickCelll" bundle:nil] forCellReuseIdentifier:@"pickCelll"];
    self.pickTable.separatorColor = [UIColor clearColor];

}


#pragma mark ---- tableDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 10000) {
        return self.contentArr.count;
    }else{
        return list_arr.count;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 10000) {
        return self.choose_btn.frame.size.height;
    }else{
        return Is_iPhone ? 80 : 120;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 10000) {
        pickCelll *cell = [self.pickTable dequeueReusableCellWithIdentifier:@"pickCelll" forIndexPath:indexPath];
        cell.pick_lab.text = [NSString stringWithFormat:@"%@",_contentArr[indexPath.row]];
        return cell;
    }else{
        testCenterCell *cell = [self.table dequeueReusableCellWithIdentifier:@"testCenterCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = list_arr[indexPath.row];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 10000) {
        chooseStr = [NSString stringWithFormat:@"%@",_contentArr[indexPath.row]];
        //注册通知1
        [[NSNotificationCenter defaultCenter]postNotificationName:@"passChooseStr" object:nil userInfo:@{@"getStr":chooseStr}];
        [self.pickTable removeFromSuperview];
        
    }else{
        //跳转详情
        TestCenterInforVC *vc = [[TestCenterInforVC alloc]init];
        TestCenterModel *model = list_arr[indexPath.row];
        vc.id = model.id;
        [self PushToViewController:vc];
    }
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
